package com.hzqy.web.ac.industry.medical;

import com.hzqy.commons.utils.ConvertBeanUtil;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.FileType;
import com.hzqy.service.industry.medical.SdGoodOrderService;
import com.hzqy.service.industry.medical.SdUserService;
import com.hzqy.web.vo.WlContentVo;
import com.hzqy.web.vo.industry.medical.GoodUserExcelVo;
import com.hzqy.web.vo.industry.medical.SdGoodOrderVo;
import com.hzqy.web.vo.industry.medical.SdUserVo;
import com.sun.xml.internal.bind.v2.TODO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/industry/DICT")
public class DICTAction {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
    @Resource
    private SdGoodOrderService sdGoodOrderService;
    @Resource
    private SdUserService sdUserService;
    
    @RequestMapping("/addDictOrder")//添加总订单中的子订单
    @ResponseBody
    public Map<String,Object> addDictOrder(SdGoodOrderVo sdGoodOrderVo){
        Map<String,Object> resultMap =new HashMap<>();
        if (sdGoodOrderVo.getF_sd_uid()==null||sdGoodOrderVo.getF_sd_village()==null||sdGoodOrderVo.getF_sd_buyTime()==null){
            resultMap.put("code","200");
            resultMap.put("message","缺少数据，无法生成订单");
        }else{
            resultMap =sdGoodOrderService.addDictOrder(sdGoodOrderVo);
        }
        return resultMap;
    }
    
    @RequestMapping("/addDictOrderTotal")//添加总订单中的总订单
    @ResponseBody//将同一个用户每天买的商品统一成一个总订单
    public Map<String,Object> addDictOrderTotal(SdGoodOrderVo sdGoodOrderVo){
        Map<String,Object> resultMap =new HashMap<>();
        if (sdGoodOrderVo.getF_sd_uid()==null||sdGoodOrderVo.getF_sd_village()==null||sdGoodOrderVo.getF_sd_buyTime()==null){
            resultMap.put("code","200");
            resultMap.put("message","缺少数据，无法生成订单");
        }else{
            resultMap =sdGoodOrderService.addDictOrderTotal(sdGoodOrderVo);
        }
        return resultMap;
    }
    
    @RequestMapping("/selectOrderByPoid")
    @ResponseBody//查询总订单下的子订单 分页
    public Map<String,Object> selectOrderByPoid(SdGoodOrderVo sdGoodOrderVo,Integer page, Integer rows){
        Map<String,Object> resultMap =sdGoodOrderService.selectOrderByPoid(sdGoodOrderVo,page,rows);
        return resultMap;
    }
    
    @RequestMapping("/selectDictTotalOrder")
    @ResponseBody//查询所有总订单(用户信息+总单价)分页
    public Map<String,Object> selectDictTotalOrder(SdUserVo sdUserVo,Integer page, Integer rows){
        Map<String,Object> resultMap =new HashMap<>();
        if (page!=null&&rows!=null){
            resultMap =sdGoodOrderService.selectDictTotalOrder(sdUserVo,page,rows);
        }
        else{
            resultMap.put("code","202");
            resultMap.put("message","缺少分页参数");
        }
        return resultMap;
    }
    
    @RequestMapping("/selectDictTotalOrderByChoose")
    @ResponseBody//根据条件查询所有总订单(用户信息+总单价)分页
    public Map<String,Object> selectDictTotalOrderByChoose(SdUserVo sdUserVo,Integer page, Integer rows){
        Map<String,Object> resultMap =new HashMap<>();
        if (page!=null&&rows!=null){
            resultMap =sdGoodOrderService.selectDictTotalOrderByChoose(sdUserVo,page,rows);
        }
        else{
            resultMap.put("code","202");
            resultMap.put("message","缺少分页参数");
        }
        return resultMap;
    }
    
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Map<String,Object> deleteOrderById(SdGoodOrderVo sdGoodOrderVo){
        Map<String,Object> resultMap =null;
        if (sdGoodOrderVo.getF_sd_isTotal()==1){//删除总订单和其子订单
            resultMap =sdGoodOrderService.deleteOrderByTotal(sdGoodOrderVo);
        }else{//删除总订单下的子订单
            resultMap =sdGoodOrderService.deleteOrderById(sdGoodOrderVo);
        }
        return resultMap;
    }
    
    @RequestMapping("/updateOrder")
    @ResponseBody
    public Map<String,Object> updateOrder(SdGoodOrderVo sdGoodOrderVo){
        Map<String,Object> resultMap =sdGoodOrderService.updateOrder(sdGoodOrderVo);
        return resultMap;
    }
    
    @RequestMapping("/insertUserInfo")//添加总订单中的子订单
    @ResponseBody
    public Map<String,Object> insertUserInfo(SdUserVo sdUserVo){
        Map<String,Object> resultMap =sdUserService.insertUserInfo(sdUserVo);
        return resultMap;
    }
    
    @RequestMapping("/updateUserInfo")//修改用户信息
    @ResponseBody
    public Map<String,Object> updateUserInfo(SdUserVo sdUserVo){
        return sdUserService.updateUserInfo(sdUserVo);
    }
    
    @RequestMapping("/selectUserById")//查询用户信息
    @ResponseBody
    public SdUserVo selectUserById(SdUserVo sdUserVo){
        return sdUserService.selectUserById(sdUserVo);
    }
    
    @RequestMapping("/insertUserInfot")//添加用户信息，初始总订单
    @ResponseBody
    public Map<String,Object> insertUserInfot(SdUserVo sdUserVo){
        return sdUserService.insertUserInfot(sdUserVo);
    }
    
    @RequestMapping("/selectOrderById")
    @ResponseBody
    public SdGoodOrderVo selectOrderById(SdGoodOrderVo sdGoodOrderVo){
        return sdGoodOrderService.selectOrderById(sdGoodOrderVo);
    }
    
    /*@RequestMapping("/insertOrders")//批量添加用户信息，初始总订单
    @ResponseBody
    public Object insertOrders(@RequestParam(value = "uploadify") MultipartFile file) throws Exception {
        //TODO:优化！优化！优化！
        if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
            StringBuffer resultStr = new StringBuffer();
            InputStream is = null;
            try {
                LOG.debug("批量导入开始");
                is = file.getInputStream();
                List<Map<String, Object>> list = ExcelUtil.parseExcel(is, ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{});
                LOG.debug("解析excel完成, 开始循环入库" + list.size());
                for (int i = 0; i < list.size(); i++) {
                    try {
                        Map<String, Object> m = list.get(i);
                        GoodUserExcelVo goodUserExcelVo = ConvertBeanUtil.getBeanFromMap(m, GoodUserExcelVo.class, GoodUserExcelVo.class.getName());
                        if (goodUserExcelVo != null) {
                            SdUserVo userVo = new SdUserVo();
                            SdGoodOrderVo orderVo = new SdGoodOrderVo();
                            userVo.setF_sd_atArea(goodUserExcelVo.getF_sd_atArea());
                            userVo.setF_sd_village(goodUserExcelVo.getF_sd_village());
                            userVo.setF_sd_address(goodUserExcelVo.getF_sd_address());
                            userVo.setF_sd_sex(goodUserExcelVo.getF_sd_sex());
                            userVo.setF_sd_surname(goodUserExcelVo.getF_sd_surname());
                            userVo.setF_sd_age(goodUserExcelVo.getF_sd_age());
                            userVo.setF_sd_phone(goodUserExcelVo.getF_sd_phone());
                            userVo.setF_sd_utype(goodUserExcelVo.getF_sd_utype());
                            userVo.setF_sd_familyNum(goodUserExcelVo.getF_sd_familyNum());
                            userVo.setF_sd_buyTime(goodUserExcelVo.getF_sd_buyTime());
                            userVo.setF_sd_orderid(goodUserExcelVo.getF_sd_orderid());
                            userVo.setF_sd_sellByDou(goodUserExcelVo.getF_sd_sellByDou());
                            Map<String, Object> map = sdUserService.insertUsersInfot(userVo);
                            if ((boolean) map.get("success")) {
                                orderVo.setF_sd_good_sys(goodUserExcelVo.getF_sd_good_sys());
                                orderVo.setF_sd_good_brand(goodUserExcelVo.getF_sd_good_brand());
                                orderVo.setF_sd_good(goodUserExcelVo.getF_sd_good());
                                orderVo.setF_sd_money(goodUserExcelVo.getF_sd_money());
                                orderVo.setF_sd_orderid(goodUserExcelVo.getF_sd_orderid());
                                orderVo.setF_sd_uid((Integer) map.get("uid"));
                                orderVo.setF_sd_village(goodUserExcelVo.getF_sd_village());
                                orderVo.setF_sd_poid((Integer) map.get("oid"));
                                orderVo.setF_sd_sellByDou(goodUserExcelVo.getF_sd_sellByDou());
                                Map<String, Object> map1 = sdGoodOrderService.addDictOrder(orderVo);
                                if (!(boolean) map1.get("success")) {
                                    resultStr.append("导入第" + (i + 1) + "条错误:" + map1.get("message"));
                                }
                            } else {
                                resultStr.append("导入第" + (i + 1) + "条错误:" + map.get("message"));
                            }
                        }
                    } catch (Exception e) {
                        LOG.error("", e);
                    }
                }
                if (resultStr.length() <= 0) {
                    resultStr.append("批量导入成功");
                }
                LOG.debug("批量完成");
            } catch (Exception e) {
                LOG.error("", e);
                resultStr.append("批量导入失败: ");
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                    }
                }
            }
            return resultStr.toString();
        } else {
            return "";
        }
    }*/
}
